package com.gaoyi.crm.util;


import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.IOnSyncListener;
import com.gaoyi.crm.callback.ISyncDownload;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.callback.OnSyncListener;
import com.gaoyi.crm.constant.HandleCode;
import com.gaoyi.crm.constant.SyncStatus;
import com.gaoyi.crm.db.dto.SyncDto;
import com.gaoyi.crm.db.query.SynClientQuery;
import com.gaoyi.crm.repository.CategoryRepository;
import com.gaoyi.crm.repository.CustomerRepository;
import com.gaoyi.crm.repository.ItemNoStoreRepository;
import com.gaoyi.crm.repository.SyncRepository;
import com.gaoyi.crm.repository.TableVersionRepository;
import com.google.gson.reflect.TypeToken;

import java.util.Date;
import java.util.List;

public class SyncDownloadUtil implements ISyncDownload {
    private static volatile SyncDownloadUtil INSTANCE;

    private IOnSyncListener mOnSyncListener;
    private final TableVersionRepository mTableVersion;
    private final SyncRepository mSync;


    public static SyncDownloadUtil getInstance() {
        if (INSTANCE == null) {
            synchronized (SyncDownloadUtil.class) {
                if (INSTANCE == null) {
                    INSTANCE = new SyncDownloadUtil();
                }
            }
        }
        return INSTANCE;
    }

    public SyncDownloadUtil() {
        mTableVersion = new TableVersionRepository();
        mSync = new SyncRepository();
    }

    public void download(String bAt, String eAt, IOnSyncListener onSyncListener) {
        this.mOnSyncListener = onSyncListener;
        ThreadManager.getInstance().execute(() -> {
            List<SynClientQuery> tableVersion = mTableVersion.getTableVersion();
            for (int i = 0; i < tableVersion.size(); i++) {
                tableVersion.get(i).setbAt(bAt);
                tableVersion.get(i).seteAt(eAt);
            }
            mSync.syncDownload(tableVersion, new OnResultListener() {
                @Override
                public void onSuccess(Object apiResult) {
                    onSyncSuccess((ApiResultData<SyncDto>) apiResult);
                }

                @Override
                public void onFails(Exception e) {
                    onSyncFails(e);
                }
            }, new TypeToken<ApiResultData<SyncDto>>() {
            }.getType());
        });
    }


    @Override
    public void onSyncSuccess(ApiResultData<SyncDto> apiResultData) {
        if (apiResultData.getStatus() != 2000) {
            downloadFail(apiResultData.getMessage());
            return;
        }
        SyncDto data = apiResultData.getData();
        if (data == null) {
            downloadSuccess(0, 0);
            return;
        }
        syncInsertTable(data);
    }

    private void syncInsertTable(SyncDto data) {
        int customerNum = new CustomerRepository().insertCustomers(data.getCustomers(), mTableVersion);
        new CategoryRepository().insertCategorys(data.getCategorys(), mTableVersion);
        int itemNoNum = new ItemNoStoreRepository().insertItemNoStores(data.getItemnostores(), mTableVersion);
        downloadSuccess(customerNum, itemNoNum);
    }

    @Override
    public void onSyncFails(Exception e) {
        downloadFail(StringUtil.getString(R.string.network_error));
    }

    private void downloadSuccess(int customerNum, int itemNoNum) {
        SyncStatus.setSync(false);
        OKHttpUtil.getInstance().post(() -> mOnSyncListener.onSyncListener(HandleCode.MSG_DOWNLOAD_TABLE_SUCCESS, StringUtil.getString(R.string.sync_success, customerNum, itemNoNum)));
    }

    private void downloadFail(String msg) {
        SyncStatus.setSync(false);
        OKHttpUtil.getInstance().post(() -> mOnSyncListener.onSyncListener(HandleCode.MSG_DOWNLOAD_TABLE_FAIL, msg));
    }
}
